layui.config({
    base: '/static/layui/'
}).extend({
    index: 'index'
}).use(['layer', 'form'], function () {
    let form = layui.form, $ = layui.$;
    getData("/sys/resource/getLayXtreeResourceByRole?roleId=" + $("#roleId").val(), function (d) {
         let layTree = new layuiXtree({
            elem: 'resources'
            , form: form
            , data: d.data
        });
         //提交表单
        form.on('submit(baseForm)', function (data) {
            //获取勾选的树
            var oCks = layTree.GetAllCheckBox(); //这是方法
            let id = $.map(oCks,function (row) {
                return row.value;
            });
            id = id.join(",");
            $.ajax({
                type: 'POST',
                data: {resourceId: id, roleId: $("#roleId").val()},
                url: "/sys/role/authResource",
                dataType: 'json',
                success: function (res) {
                    if (res.code == "200"){
                        layer.open({
                            content: '操作成功'
                            ,closeBtn: false
                            ,btn: ['确定']
                            ,yes: function(index){
                                var pIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(pIndex);//关闭弹出的子页面窗口
                                parent.location.reload();
                                layer.close(index);
                            }
                        });
                    }else{
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            })
            return false;
        });
    });
});